package com.stu.serive.impl;

import com.stu.Dto.StayApplicationDTO;
import com.stu.constant.StatusConstant;
import com.stu.context.BaseContext;
import com.stu.mapper.StayApplicationMapper;
import com.stu.pojo.StayApplication;
import com.stu.serive.StayApplicationService;
import com.stu.serive.aTService;
import com.stu.vo.StayApplicationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class StayApplicationImpl implements StayApplicationService {

    @Autowired
    private StayApplicationMapper stayApplicationMapper;

    /**
     * 添加学生申请留校
     * @param stayApplicationDTO
     */
    public void insertStayApplication(StayApplicationDTO stayApplicationDTO) {
        //获取学生id
        Long currentId = BaseContext.getCurrentId();
        //创建实体
        StayApplication stayApplication = new StayApplication();
        //赋值
        BeanUtils.copyProperties(stayApplicationDTO, stayApplication);
        stayApplication.setStuId(Math.toIntExact(currentId));
        stayApplication.setApplicationTime(LocalDateTime.now());
        //添加操作
        stayApplicationMapper.insertStayApplication(stayApplication);
    }

    /**
     * 查询留校申请进度
     * @return
     */
    public List<StayApplicationVo> selectStayApplication() {
        //获取学生id
        Long currentId = BaseContext.getCurrentId();
        //根据学生id查询 留校申请表和学生基础信息表 获取Vo数据
        List<StayApplicationVo> stayApplicationVoList = stayApplicationMapper.selectStayApplicationWithStu(currentId);
        //log.info("stayApplicationVoList:{}",stayApplicationVoList);
        //对审核状态进行判断
        for (StayApplicationVo stayApplicationVo : stayApplicationVoList) {
            if (Objects.equals(stayApplicationVo.getStatus(), "1")){
                stayApplicationVo.setStatus(StatusConstant.STATUS_1);
            } else if (Objects.equals(stayApplicationVo.getStatus(), "0")) {
                stayApplicationVo.setStatus(StatusConstant.STATUS_0);
            }else {
                stayApplicationVo.setStatus(StatusConstant.STATUS_2);
            }


        }
        return stayApplicationVoList;
    }
}
